package top.vanchor.flexible.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;
import top.vanchor.flexible.common.Res;
import top.vanchor.flexible.entity.*;
import top.vanchor.flexible.entity.dto.AcadeAchieveListDto;
import top.vanchor.flexible.entity.dto.AcadeActiveListDto;
import top.vanchor.flexible.entity.dto.ExpertListDto;
import top.vanchor.flexible.entity.dto.OriginalListDto;
import top.vanchor.flexible.entity.vo.ArtistWorksVo;
import top.vanchor.flexible.service.*;
import top.vanchor.flexible.utils.ImageAddress;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Log4j2
@Api("前端App请求控制器-无需token")
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private ISpecialService iSpecialService;

    @Resource
    private IProjIntroductionService iProjIntroductionService;

    @Resource
    private IAcadeActiveService iAcadeActiveService;

    @Resource
    private IAcadeAchieveService iAcadeAchieveService;

    @Resource
    private IArtistService iArtistService;

    @Resource
    private IResourcePathService iResourcePathService;

    @Resource
    private IWorksService iWorksService;

    @Resource
    private IAlbumService iAlbumService;

    @Resource
    private IExpertIntroductionService iExpertIntroductionService;

    @Resource
    private IOriginalService iOriginalService;

    @Resource
    private ICategoryService iCategoryService;

    @Resource
    private ICategoryItemService iCategoryItemService;


    // finish
    @ApiOperation("获取所有专题")
    @ResponseBody
    @GetMapping("/getSpecialList")
    public Res<List<Special>> getSpecialList() {
        return Res.success(iSpecialService.list());
    }

    // finish
    @ApiOperation("获取项目简介")
    @ResponseBody
    @GetMapping("/getProjIntroductionList")
    public Res<ProjIntroduction> getProjIntroductionList() {
        List<ProjIntroduction> projIntroductions= iProjIntroductionService.list(new LambdaQueryWrapper<ProjIntroduction>().orderByDesc(ProjIntroduction::getId));
        ProjIntroduction projIntroduction = null;
        if(projIntroductions.size()>0){
            projIntroduction= projIntroductions.get(0);
        }
        return Res.success(projIntroduction);
    }

    // finish
    @ApiOperation("获取学术成果列表")
    @ResponseBody
    @PostMapping("/getAcadeAchieveList")
    public Res<IPage<AcadeAchieve>> getAcadeAchieveList(@RequestBody AcadeAchieveListDto dto) {
        return Res.success(iAcadeAchieveService.getList(dto));
    }

    // finish
    @ApiOperation("获取学术活动列表")
    @ResponseBody
    @PostMapping("/getAcadeActiveList")
    public Res<IPage<AcadeActive>> getAcadeActiveList(@RequestBody AcadeActiveListDto dto) {
        return Res.success(iAcadeActiveService.getList(dto));
    }
    @ApiOperation("获取学术活动详情")
    @ResponseBody
    @GetMapping("/getActivityDetail")
    public Res<AcadeActive> getActivityDetail(@RequestParam("id") Integer id) {
        AcadeActive acadeActive = iAcadeActiveService.getOne(new LambdaQueryWrapper<AcadeActive>().eq(AcadeActive::getId, id));
        return Res.success(acadeActive);
    }
    // finish
    @ApiOperation("获取专题下的所有艺人")
    @ResponseBody
    @GetMapping("/getArtistList")
    public Res<List<Artist>> getArtistList(@RequestParam("id") Integer id) {
        List<Artist> list = iArtistService.list(new LambdaQueryWrapper<Artist>().eq(Artist::getSpecialId, id));
        for (Artist artist : list) {
            artist.setAvatarUrl(getResourcePathById(artist.getAvatarId()));
        }
        return Res.success(list);
    }

    @ApiOperation("获取艺人详情")
    @ResponseBody
    @GetMapping("/getArtistInfo")
    public Res<ArtistWorksVo> getArtistInfo(@RequestParam("id") Integer id,@RequestParam("albumId") Integer albumId) {
        //获取艺人详情
        Artist artist = iArtistService.getOne(new LambdaQueryWrapper<Artist>().eq(Artist::getId, id));
        artist.setAvatarUrl(getResourcePathById(artist.getAvatarId()));
        //获取艺人作品
        //1音频；2视频；3pdf；4图片
        List<Works> list = iWorksService.list(new LambdaQueryWrapper<Works>()
                .eq(Works::getArtistId, id)
                .eq(albumId!=0,Works::getAlbumId,albumId)
        );
        ArtistWorksVo artistWorksVo = new ArtistWorksVo();
        for (Works works : list) {
            works.setResourceUrl(getResourcePathById(works.getResourceId()));
            works.setCoverImgUrl(getResourcePathById(works.getCoverImgId()));
            works.setLyricIntroductionUrl(getResourcePathById(works.getLyricIntroduction()));
            works.setScoreIntroductionUrl(getResourcePathById(works.getScoreIntroduction()));
            if(works.getType() == 1){
                artistWorksVo.getAudioList().add(works);
            }else if(works.getType() == 2){
                artistWorksVo.getVideoList().add(works);
            }else if(works.getType() == 3){
                artistWorksVo.getPdfList().add(works);
            }else if(works.getType() == 4){
                artistWorksVo.getImageList().add(works);
            }
            
        }
        artistWorksVo.setArtist(artist);
        return Res.success(artistWorksVo);
    }

    @ApiOperation("获取艺人的所有专辑")
    @ResponseBody
    @GetMapping("/getAlbumListByArtList")
    public Res<List<Album>> getAlbumListByArtList(@RequestParam("artistId") Integer artistId) {
        List<Album> list = iAlbumService.list(new LambdaQueryWrapper<Album>().eq(Album::getArtistId, artistId));
        return Res.success(list);
    }

    // finish
    @ApiOperation("获取专家团队列表")
    @ResponseBody
    @PostMapping("/getExpertIntroductionList")
    public Res<IPage<ExpertIntroduction>> getExpertIntroductionList(@RequestBody ExpertListDto expertListDto) {
        IPage<ExpertIntroduction> list = iExpertIntroductionService.getList(expertListDto);
        for (ExpertIntroduction expertIntroduction : list.getRecords()) {
            expertIntroduction.setAvatarUrl(getResourcePathById(expertIntroduction.getAvatarId()));
        }
        return Res.success(list);
    }

    // finish
    @ApiOperation("获取专家详情")
    @ResponseBody
    @GetMapping("/getExpertIntroductionInfo")
    public Res<ExpertIntroduction> getExpertIntroductionInfo(@RequestParam("id") Integer id) {
        ExpertIntroduction expertIntroduction = iExpertIntroductionService.getOne(new LambdaQueryWrapper<ExpertIntroduction>().eq(ExpertIntroduction::getId, id));
        expertIntroduction.setAvatarUrl(getResourcePathById(expertIntroduction.getAvatarId()));
        return Res.success(expertIntroduction);
    }

    @ApiOperation("获取作品详情")
    @ResponseBody
    @GetMapping("/getWorksInfo")
    public Res<Works> getWorksInfo(@RequestParam("id") Integer id) {
        Works works = iWorksService.getOne(new LambdaQueryWrapper<Works>().eq(Works::getId, id));
        Artist artist = iArtistService.getOne(new LambdaQueryWrapper<Artist>().eq(Artist::getId, works.getArtistId()));
        works.setResourceUrl(getResourcePathById(works.getResourceId()));
        works.setCoverImgUrl(getResourcePathById(works.getCoverImgId()));
        works.setLyricIntroductionUrl(getResourcePathById(works.getLyricIntroduction()));
        works.setScoreIntroductionUrl(getResourcePathById(works.getScoreIntroduction()));
        works.setArtistDesc(artist.getIntroduction());
        return Res.success(works);
    }

    @ApiOperation("获取文本列表")
    @ResponseBody
    @PostMapping("/getOriginalList")
    public Res<IPage<Original>> getOriginalList(@RequestBody OriginalListDto originalListDto) {
        IPage<Original> list = iOriginalService.getList(originalListDto);
        for (Original record : list.getRecords()) {
            record.setResourceUrl(getResourcePathById(record.getResourceId()));
        }
        return Res.success(list);
    }

    @ApiOperation("获取扩展资料或田野资料分类列表")
    @ResponseBody
    @GetMapping("/getCategoryList")
    public Res<HashMap<String, Object>> getCategoryList(@RequestParam("specialId") Integer specialId) {
        List<Category> list = iCategoryService.list(new LambdaQueryWrapper<Category>().eq(Category::getSpecialId, specialId));
        for (Category category : list) {
            category.setImgUrl(getResourcePathById(category.getImgId()));
            List<CategoryItem> itemList = iCategoryItemService.list(new LambdaQueryWrapper<CategoryItem>().eq(CategoryItem::getCategoryId, category.getId()));
            for (CategoryItem categoryItem : itemList) {
                categoryItem.setResourceUrl(getResourcePathById(categoryItem.getResourceId()));
            }
            category.setItemList(itemList);
        }
        Special special = iSpecialService.getOne(new LambdaQueryWrapper<Special>().eq(Special::getId, specialId));
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",list);
        map.put("special",special);
        return Res.success(map);
    }

    @ApiOperation("获取扩展资料或田野资料资源详情")
    @ResponseBody
    @GetMapping("/getCategoryInfo")
    public Res<Category> getCategoryInfo(@RequestParam("categoryItemId") Integer categoryItemId, @RequestParam("resourceId") Integer resourceId) {
        CategoryItem categoryItem = iCategoryItemService.getOne(new LambdaQueryWrapper<CategoryItem>().eq(CategoryItem::getId, categoryItemId).eq(CategoryItem::getResourceId, resourceId));
        Category category = iCategoryService.getOne(new LambdaQueryWrapper<Category>().eq(Category::getId, categoryItem.getCategoryId()));
        categoryItem.setResourceUrl(getResourcePathById(categoryItem.getResourceId()));
        category.setImgUrl(getResourcePathById(category.getImgId()));
        category.setCategoryItem(categoryItem);
        return Res.success(category);
    }

    public String getResourcePathById(Integer id){
        ResourcePath resourcePath = iResourcePathService.getOne(new LambdaQueryWrapper<ResourcePath>()
                .eq(ResourcePath::getRId, id));
        String path = "";
        if(!Objects.isNull(resourcePath)){
            path = ImageAddress.getAllPath(resourcePath.getUrl());
        }
        return path;
    }
}
